﻿using BingoClientModel;
using BingoServerCommon.Common;
using Dapper;
using MySql.Data.MySqlClient;


namespace BingoHallServerConsole.Business.Account
{
	/// <inheritdoc />
	public class Login : Package
	{


		public Login(EnumPackCommand mainid) : base(mainid)
		{
		}

		public Login(byte[] buffer, int len) : base(buffer, len)
		{
		}

		public override Package Clone(byte[] bytes)
		{
			return new Login(bytes, bytes.Length);
		}



		/// <summary>
		/// 
		/// </summary>
		public override void Excute(Session session)
		{
			var account = ReadString();
			var password = ReadString();

			MySqlConnection conn = Program.ConnectionPool.getConnection(); //从连接池拿到一个连接
			string sql = $"SELECT * from players p WHERE p.account = '{account}' and p.password_hash = '{password}';";

			var package = new Package(EnumPackCommand.SC_Login_Ret);
			var player = conn.QueryFirstOrDefault<Player>(sql);

			if (player != null)
			{
				package.Write((byte)EnumReqRetStatus.Success);
			}
			else
			{
				package.Write((byte)EnumReqRetStatus.Fail0);
			}
			//session.Send(package);

			Program.ConnectionPool.returnConnection(conn);//连接用完回收
		}
	}
}
